﻿using System.Runtime.Serialization;
using System.Xml;

using Core.Model.Hierarchy;

namespace Core.Logic
{
    public class SerializationManager
    {
        public void SaveProject(Project project, string path)
        {
            var settings = new XmlWriterSettings { Indent = true };

            using (var writer = XmlWriter.Create(path, settings))
            {
                var serializer = new DataContractSerializer(typeof(Project));
                serializer.WriteObject(writer, project);  
           
                writer.Flush();
            }
        }

        public Project LoadProject(string path)
        {
            using (var reader = XmlReader.Create(path))
            {
                var deserializer = new DataContractSerializer(typeof(Project));

                var project = (Project)deserializer.ReadObject(reader);
                project.Path = path;

                return project;
            }
        }
    }
}